/*
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
* gazelle is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*     http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/

#ifndef __LSTACK_SOCK_HTABLE_H__
#define __LSTACK_SOCK_HTABLE_H__

#include <lwip/lwipgz_hlist.h>
#include <stdint.h>
#include <rte_rwlock.h>

#include "common/gazelle_opt.h"
struct sock_htable_key {
    uint32_t ip[4];
    uint32_t proto;
    uint16_t port;
} __rte_packed;

struct sock_htable_node {
    struct sock_htable_key key;

    uint16_t queue_id;
    uint16_t cur_conn;

    struct hlist_node hlist_node;
};

struct sock_htable_bucket {
    uint32_t chain_size;
    struct hlist_head chain;
};

struct sock_htable {
    rte_rwlock_t rwlock;
    uint32_t cur_sock_num;
    uint32_t max_sock_num;
    struct sock_htable_bucket array[GAZELLE_MAX_TCP_SOCK_HTABLE_SIZE];
};

struct sock_htable *sock_htable_create(uint32_t max_sock_num);
struct sock_htable *sock_htable_attach(void);
void sock_htable_destroy(void);
int sock_htable_add_key_data(struct sock_htable_key *key, uint16_t queue_id);
int sock_htable_del_key(struct sock_htable_key *key, uint16_t queue_id);
int sock_htable_get_by_min_conn(struct sock_htable_key *key, uint16_t cur_queue_id, bool create);
void sock_htable_dump(void);
void sock_htable_del_by_queue(uint16_t queue_id);

#endif
